#include "Terrain.h"
#include <assert.h>

void InitialisationTerrain (Terrain * t, int dimx, int dimy)
{
	int i;
	int j;
	(*t).dimx = dimx;
	(*t).scrollx = 0;
	(*t).dimy = dimy;
	(*t).scrolly = 0;
	(*t).cases = (Cases*) malloc ((*t).dimx* (*t).dimy* sizeof (Cases));
	for (i = 0 ; i < (*t).dimy; i++)
	{
		for (j = 0;j < (*t).dimx; j++)
		{
			InitialisationCases (&(*t).cases[i*(*t).dimx+j]);	
		}
	}
}

Cases * ObtientCasesTerrain (Terrain * t, int x, int y)
{
	assert( 0<=x && x<(*t).dimx);
	assert( 0<=y  && y<(*t).dimy); 
	return (&(*t).cases[y*(*t).dimx+x]);	
}

const Cases * ObtientConstCasesTerrain (const Terrain * t, int x, int y)
{
	assert( 0<=x && x<(*t).dimx);
	assert( 0<=y  && y<(*t).dimy); 
	return (&(*t).cases[y*(*t).dimx+x]);	
}

void DestructionTerrain (Terrain * t)
{
	int i;
	int j;
	for (i = 0 ; i < (*t).dimy; i++)
	{
		for (j = 0;j < (*t).dimx; j++)
		{
			DestructionCases (&(*t).cases[i*(*t).dimx+j]);	
		}
	}
	(*t).dimx = 0;
	(*t).dimy = 0;
	free ((*t).cases);
	(*t).cases = NULL;
}

void TerrainTestRegression()
{
	Terrain * ttest = (Terrain*) malloc (sizeof (Terrain));
	Cases * c;
	const Cases * c2;
	int i;
	int j;
	

	/* Vérification de la procédure InitialisationTerrain */
	InitialisationTerrain (ttest,16,10);
	assert ( (*ttest).dimx == 16);
	assert ( (*ttest).dimy == 10);
	assert ( (*ttest).cases != NULL);
	
	/* Vérification de la procédure ObtientCasesTerrain*/
	for (i=0; i< 10 ; i++)
	{
		for (j = 0; j< 16; j++)
		{
			c = ObtientCasesTerrain (ttest,j, i);
			assert( c == (&(*ttest).cases[i*(*ttest).dimx+j]));
		}
	}
	
	/* Vérification de la procédure ObtientConstCasesTerrain*/
	for (i=0; i< 10 ; i++)
	{
		for (j = 0; j< 16; j++)
		{
			c2 = ObtientConstCasesTerrain (ttest,j, i);
			assert( c2 == (&(*ttest).cases[i*(*ttest).dimx+j]));
		}
	}
	
	/* Vérification de la procédure DestructionTerrain */
	DestructionTerrain (ttest);
	assert ( (*ttest).dimx == 0);
	assert ( (*ttest).dimy == 0);
	assert ( (*ttest).cases == NULL);
	
	free(ttest);
	
}
